﻿using System;

namespace Cartographer.Builder
{
	public class Vector2
	{
		public float X;
		public float Y;

		public Vector2() { }

		public Vector2(float x, float y)
		{
			X = x;
			Y = y;
		}

		public Vector2(int x, int y)
		{
			X = x;
			Y = y;
		}

		public float LengthSquared()
		{
			return X * X + Y * Y;
		}

		public static Vector2 Normalize(Vector2 value)
		{
			var vector2 = new Vector2();
			var x = value.X * value.X + value.Y * value.Y;
			var single = 1f / (float)Math.Sqrt((double)x);
			vector2.X = value.X * single;
			vector2.Y = value.Y * single;
			return vector2;
		}

		public void Normalize()
		{
			var x = X * X + Y * Y;
			var single = 1f / (float)Math.Sqrt((double)x);
			var vector2 = this;
			vector2.X = vector2.X * single;
			var y = this;
			y.Y = y.Y * single;
		}

		public static void Normalize(ref Vector2 value, out Vector2 result)
		{
			result = new Vector2();
			var x = value.X * value.X + value.Y * value.Y;
			var single = 1f / (float)Math.Sqrt((double)x);
			result.X = value.X * single;
			result.Y = value.Y * single;
		}

		public static float Dot(Vector2 value1, Vector2 value2)
		{
			return value1.X * value2.X + value1.Y * value2.Y;
		}

		public static Vector2 operator +(Vector2 value1, Vector2 value2)
		{
			var vector2 = new Vector2();
			vector2.X = value1.X + value2.X;
			vector2.Y = value1.Y + value2.Y;
			return vector2;
		}
	}
}
